home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / Src / Ch6 / Contrast.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1999-04-25  |  12.5 KB  |  384 lines

  1. VERSION 5.00
  2. Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "COMDLG32.OCX"
  3. Begin VB.Form frmContrast 
  4.    Caption         =   "Contrast []"
  5.    ClientHeight    =   4800
  6.    ClientLeft      =   165
  7.    ClientTop       =   735
  8.    ClientWidth     =   9120
  9.    LinkTopic       =   "Form2"
  10.    ScaleHeight     =   4800
  11.    ScaleWidth      =   9120
  12.    StartUpPosition =   3  'Windows Default
  13.    Begin VB.PictureBox picHistogram 
  14.       Height          =   1455
  15.       Index           =   2
  16.       Left            =   6120
  17.       ScaleHeight     =   93
  18.       ScaleMode       =   3  'Pixel
  19.       ScaleWidth      =   188
  20.       TabIndex        =   7
  21.       Top             =   0
  22.       Width           =   2880
  23.    End
  24.    Begin VB.PictureBox picHistogram 
  25.       Height          =   1455
  26.       Index           =   1
  27.       Left            =   3120
  28.       ScaleHeight     =   93
  29.       ScaleMode       =   3  'Pixel
  30.       ScaleWidth      =   188
  31.       TabIndex        =   6
  32.       Top             =   0
  33.       Width           =   2880
  34.    End
  35.    Begin MSComDlg.CommonDialog dlgOpenFile 
  36.       Left            =   0
  37.       Top             =   840
  38.       _ExtentX        =   847
  39.       _ExtentY        =   847
  40.       _Version        =   393216
  41.    End
  42.    Begin VB.PictureBox picHistogram 
  43.       Height          =   1455
  44.       Index           =   0
  45.       Left            =   120
  46.       ScaleHeight     =   93
  47.       ScaleMode       =   3  'Pixel
  48.       ScaleWidth      =   188
  49.       TabIndex        =   5
  50.       Top             =   0
  51.       Width           =   2880
  52.    End
  53.    Begin VB.CommandButton cmdAdjust 
  54.       Caption         =   "Adjust"
  55.       Height          =   375
  56.       Left            =   5160
  57.       TabIndex        =   4
  58.       Top             =   1500
  59.       Width           =   855
  60.    End
  61.    Begin VB.HScrollBar hbarBrightness 
  62.       Height          =   255
  63.       Left            =   120
  64.       Max             =   1000
  65.       TabIndex        =   2
  66.       Top             =   1560
  67.       Width           =   4335
  68.    End
  69.    Begin VB.PictureBox picOriginal 
  70.       AutoSize        =   -1  'True
  71.       Height          =   2775
  72.       Left            =   120
  73.       ScaleHeight     =   181
  74.       ScaleMode       =   3  'Pixel
  75.       ScaleWidth      =   157
  76.       TabIndex        =   1
  77.       Top             =   1920
  78.       Width           =   2415
  79.    End
  80.    Begin VB.PictureBox picResult 
  81.       Height          =   2775
  82.       Left            =   2640
  83.       ScaleHeight     =   181
  84.       ScaleMode       =   3  'Pixel
  85.       ScaleWidth      =   157
  86.       TabIndex        =   0
  87.       Top             =   1920
  88.       Width           =   2415
  89.    End
  90.    Begin VB.Label lblBrighhtness 
  91.       Alignment       =   1  'Right Justify
  92.       BorderStyle     =   1  'Fixed Single
  93.       Caption         =   "0"
  94.       Height          =   255
  95.       Left            =   4560
  96.       TabIndex        =   3
  97.       Top             =   1560
  98.       Width           =   495
  99.    End
  100.    Begin VB.Menu mnuFile 
  101.       Caption         =   "&File"
  102.       Begin VB.Menu mnuFileOpen 
  103.          Caption         =   "&Open..."
  104.          Shortcut        =   ^O
  105.       End
  106.       Begin VB.Menu mnuFileSaveAs 
  107.          Caption         =   "Save &As..."
  108.          Shortcut        =   ^A
  109.       End
  110.    End
  111. Attribute VB_Name = "frmContrast"
  112. Attribute VB_GlobalNameSpace = False
  113. Attribute VB_Creatable = False
  114. Attribute VB_PredeclaredId = True
  115. Attribute VB_Exposed = False
  116. Option Explicit
  117. Private MinIndex(0 To 2) As Integer
  118. Private MaxIndex(0 To 2) As Integer
  119. ' Arrange the controls.
  120. Private Sub ArrangeControls()
  121. Dim wid As Single
  122.     ' Position the result PictureBox.
  123.     picResult.Move _
  124.         picOriginal.Left + picOriginal.Width + 120, _
  125.         picOriginal.Top, _
  126.         picOriginal.Width, _
  127.         picOriginal.Height
  128.     picResult.Cls
  129.     ' This makes the image resize itself to
  130.     ' fit the picture.
  131.     picResult.Picture = picResult.Image
  132.     ' Make the form big enough.
  133.     wid = picResult.Left + picResult.Width
  134.     If wid < picHistogram(2).Left + picHistogram(2).Width Then _
  135.         wid = picHistogram(2).Left + picHistogram(2).Width
  136.     Width = wid + Width - ScaleWidth + 120
  137.     Height = picResult.Top + picResult.Height + _
  138.         Height - ScaleHeight + 120
  139.     DoEvents
  140. End Sub
  141. ' Transform the image.
  142. Private Sub TransformImage()
  143. Dim pixels() As RGBTriplet
  144. Dim bits_per_pixel As Integer
  145. Dim r_mid As Integer
  146. Dim g_mid As Integer
  147. Dim b_mid As Integer
  148. Dim r_scale As Single
  149. Dim g_scale As Single
  150. Dim b_scale As Single
  151. Dim r_diff As Integer
  152. Dim g_diff As Integer
  153. Dim b_diff As Integer
  154. Dim r As Integer
  155. Dim g As Integer
  156. Dim b As Integer
  157. Dim X As Integer
  158. Dim Y As Integer
  159.     ' Get the pixels from picOriginal.
  160.     GetBitmapPixels picOriginal, pixels, bits_per_pixel
  161.     ' Get the middle values for the components.
  162.     r_mid = (MaxIndex(0) + MinIndex(0)) / 2
  163.     g_mid = (MaxIndex(1) + MinIndex(1)) / 2
  164.     b_mid = (MaxIndex(2) + MinIndex(2)) / 2
  165.     ' Calculate the scale factors needed to resize
  166.     ' the color values.
  167.     r_scale = hbarBrightness.value / (MaxIndex(0) - MinIndex(0))
  168.     g_scale = hbarBrightness.value / (MaxIndex(1) - MinIndex(1))
  169.     b_scale = hbarBrightness.value / (MaxIndex(2) - MinIndex(2))
  170.     ' Set the colors for each component separately.
  171.     For Y = 0 To picOriginal.ScaleHeight - 1
  172.         For X = 0 To picOriginal.ScaleWidth - 1
  173.             With pixels(X, Y)
  174.                 r_diff = .rgbRed - r_mid
  175.                 r_diff = r_diff * r_scale
  176.                 r = 127 + r_diff
  177.                 If r < 0 Then r = 0
  178.                 If r > 255 Then r = 255
  179.                 .rgbRed = r
  180.                 g_diff = .rgbGreen - g_mid
  181.                 g_diff = g_diff * g_scale
  182.                 g = 127 + g_diff
  183.                 If g < 0 Then g = 0
  184.                 If g > 255 Then g = 255
  185.                 .rgbGreen = g
  186.                 b_diff = .rgbBlue - b_mid
  187.                 b_diff = b_diff * b_scale
  188.                 b = 127 + b_diff
  189.                 If b < 0 Then b = 0
  190.                 If b > 255 Then b = 255
  191.                 .rgbBlue = b
  192.             End With
  193.         Next X
  194.     Next Y
  195.     ' Set picResult's pixels.
  196.     SetBitmapPixels picResult, bits_per_pixel, pixels
  197.     picResult.Picture = picResult.Image
  198.     ' Show the new brightness histogram.
  199.     ShowHistograms picResult, False
  200. End Sub
  201. ' Show the component histograms.
  202. Private Sub ShowHistograms(ByVal picImage As PictureBox, ByVal save_min_max As Boolean)
  203. Dim counts(0 To 2, 0 To 255) As Long
  204. Dim max_count As Long
  205. Dim brightness As Integer
  206. Dim pixels() As RGBTriplet
  207. Dim bits_per_pixel As Integer
  208. Dim X As Integer
  209. Dim Y As Integer
  210. Dim i As Integer
  211. Dim j As Integer
  212.     ' Clear the previous results.
  213.     For i = 0 To 2
  214.         picHistogram(i).Cls
  215.         picHistogram(i).Refresh
  216.     Next i
  217.     ' Get the pixels from picImage.
  218.     GetBitmapPixels picImage, pixels, bits_per_pixel
  219.     ' Count the brightness values.
  220.     For Y = 0 To picImage.ScaleHeight - 1
  221.         For X = 0 To picImage.ScaleWidth - 1
  222.             With pixels(X, Y)
  223.                 counts(0, .rgbRed) = counts(0, .rgbRed) + 1
  224.                 counts(1, .rgbGreen) = counts(1, .rgbGreen) + 1
  225.                 counts(2, .rgbBlue) = counts(2, .rgbBlue) + 1
  226.             End With
  227.         Next X
  228.     Next Y
  229.     ' Find the largest count value.
  230.     For i = 0 To 2
  231.         ' Skip value 0. There tend to be a lot of
  232.         ' them and they dominate things.
  233.         For j = 1 To 255
  234.             If max_count < counts(i, j) _
  235.                 Then max_count = counts(i, j)
  236.         Next j
  237.     Next i
  238.     ' Display the brightness histograms.
  239.     For i = 0 To 2
  240.         picHistogram(i).ScaleTop = 1.1 * max_count
  241.         picHistogram(i).ScaleHeight = -1.2 * max_count
  242.         picHistogram(i).ScaleLeft = -1
  243.         picHistogram(i).ScaleWidth = 258
  244.         For brightness = 0 To 255
  245.             If counts(i, brightness) > 0 Then _
  246.                 picHistogram(i).Line (brightness, 0)-(brightness + 1, counts(i, brightness)), , BF
  247.         Next brightness
  248.     Next i
  249.     ' Find the largest and smallest non-zero counts.
  250.     If save_min_max Then
  251.         For i = 0 To 2
  252.             MinIndex(i) = 255
  253.             For brightness = 0 To 255
  254.                 If counts(i, brightness) > 0 Then
  255.                     MinIndex(i) = brightness
  256.                     Exit For
  257.                 End If
  258.             Next brightness
  259.             MaxIndex(i) = 0
  260.             For brightness = 255 To 0 Step -1
  261.                 If counts(i, brightness) > 0 Then
  262.                     MaxIndex(i) = brightness
  263.                     Exit For
  264.                 End If
  265.             Next brightness
  266.         Next i
  267.     End If
  268. End Sub
  269. ' Transform the image.
  270. Private Sub cmdAdjust_Click()
  271.     If picResult.Picture <> 0 Then
  272.         Screen.MousePointer = vbHourglass
  273.         DoEvents
  274.         TransformImage
  275.         Screen.MousePointer = vbDefault
  276.     End If
  277. End Sub
  278. ' Start in the current directory.
  279. Private Sub Form_Load()
  280. Dim i As Integer
  281.     picOriginal.AutoSize = True
  282.     picOriginal.ScaleMode = vbPixels
  283.     picOriginal.AutoRedraw = True
  284.     picResult.ScaleMode = vbPixels
  285.     picResult.AutoRedraw = True
  286.     For i = 0 To 2
  287.         picHistogram(i).AutoRedraw = True
  288.     Next i
  289.     dlgOpenFile.CancelError = True
  290.     dlgOpenFile.InitDir = App.Path
  291.     dlgOpenFile.Filter = _
  292.         "Bitmaps (*.bmp)|*.bmp|" & _
  293.         "GIFs (*.gif)|*.gif|" & _
  294.         "JPEGs (*.jpg)|*.jpg;*.jpeg|" & _
  295.         "Icons (*.ico)|*.ico|" & _
  296.         "Cursors (*.cur)|*.cur|" & _
  297.         "Run-Length Encoded (*.rle)|*.rle|" & _
  298.         "Metafiles (*.wmf)|*.wmf|" & _
  299.         "Enhanced Metafiles (*.emf)|*.emf|" & _
  300.         "Graphic Files|*.bmp;*.gif;*.jpg;*.jpeg;*.ico;*.cur;*.rle;*.wmf;*.emf|" & _
  301.         "All Files (*.*)|*.*"
  302. End Sub
  303. ' Display the brightness value selected.
  304. Private Sub hbarBrightness_Change()
  305.     lblBrighhtness.Caption = Format$(hbarBrightness.value)
  306. End Sub
  307. ' Display the brightness value selected.
  308. Private Sub hbarBrightness_Scroll()
  309.     lblBrighhtness.Caption = Format$(hbarBrightness.value)
  310. End Sub
  311. ' Load the indicated file.
  312. Private Sub mnuFileOpen_Click()
  313. Dim file_name As String
  314.     ' Let the user select a file.
  315.     On Error Resume Next
  316.     dlgOpenFile.Flags = cdlOFNFileMustExist + cdlOFNHideReadOnly
  317.     dlgOpenFile.ShowOpen
  318.     If Err.Number = cdlCancel Then
  319.         Exit Sub
  320.     ElseIf Err.Number <> 0 Then
  321.         Beep
  322.         MsgBox "Error selecting file.", , vbExclamation
  323.         Exit Sub
  324.     End If
  325.     On Error GoTo 0
  326.     Screen.MousePointer = vbHourglass
  327.     DoEvents
  328.     file_name = Trim$(dlgOpenFile.FileName)
  329.     dlgOpenFile.InitDir = Left$(file_name, Len(file_name) _
  330.         - Len(dlgOpenFile.FileTitle) - 1)
  331.     Caption = "Contrast [" & dlgOpenFile.FileTitle & "]"
  332.     ' Open the original file.
  333.     On Error GoTo LoadError
  334.     picOriginal.Picture = LoadPicture(file_name)
  335.     On Error GoTo 0
  336.     ' Make picResult the same size and position it.
  337.     ArrangeControls
  338.     ' Make picResult show the same image.
  339.     picResult.Picture = picOriginal.Picture
  340.     DoEvents
  341.     ' Display the brightness histogram.
  342.     ShowHistograms picOriginal, True
  343.     Screen.MousePointer = vbDefault
  344.     Exit Sub
  345. LoadError:
  346.     Screen.MousePointer = vbDefault
  347.     MsgBox "Error " & Format$(Err.Number) & _
  348.         " opening file '" & file_name & "'" & vbCrLf & _
  349.         Err.Description
  350. End Sub
  351. ' Save the transformed image.
  352. Private Sub mnuFileSaveAs_Click()
  353. Dim file_name As String
  354.     ' Let the user select a file.
  355.     On Error Resume Next
  356.     dlgOpenFile.Flags = cdlOFNOverwritePrompt + cdlOFNHideReadOnly
  357.     dlgOpenFile.ShowSave
  358.     If Err.Number = cdlCancel Then
  359.         Exit Sub
  360.     ElseIf Err.Number <> 0 Then
  361.         Beep
  362.         MsgBox "Error selecting file.", , vbExclamation
  363.         Exit Sub
  364.     End If
  365.     On Error GoTo 0
  366.     Screen.MousePointer = vbHourglass
  367.     DoEvents
  368.     file_name = Trim$(dlgOpenFile.FileName)
  369.     dlgOpenFile.InitDir = Left$(file_name, Len(file_name) _
  370.         - Len(dlgOpenFile.FileTitle) - 1)
  371.     Caption = "Contrast [" & dlgOpenFile.FileTitle & "]"
  372.     ' Save the transformed image into the file.
  373.     On Error GoTo SaveError
  374.     SavePicture picResult.Picture, file_name
  375.     On Error GoTo 0
  376.     Screen.MousePointer = vbDefault
  377.     Exit Sub
  378. SaveError:
  379.     Screen.MousePointer = vbDefault
  380.     MsgBox "Error " & Format$(Err.Number) & _
  381.         " saving file '" & file_name & "'" & vbCrLf & _
  382.         Err.Description
  383. End Sub
  384.